<?php

namespace app\configure\controller;

use app\configure\model\ConfigKitchenToolModel;
use think\admin\Controller;
use think\admin\extend\DataExtend;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class KitchenTool extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private string $table = 'ConfigKitchenTool';

    /**
     * 共享厨房工具管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = "共享厨房工具管理";
        $query = $this->_query(ConfigKitchenToolModel::class)->like('name')->dateBetween('create_time');
        $query->equal('type')->order('id asc')->page(false);
    }

    /**
     * 列表数据处理
     * @param array $data
     */
    protected function _index_page_filter(array &$data)
    {
        foreach ($data as &$vo) {
            $vo['ids'] = join(',', DataExtend::getArrSubIds($data, $vo['id'], 'id', 'nid'));
        }
        $data = DataExtend::arr2table($data, 'id', 'nid');
    }

    /**
     * 添加共享厨房工具
     * @auth true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function add()
    {
        $this->_form(ConfigKitchenToolModel::class, 'form');
    }

    /**
     * 编辑共享厨房工具
     * @auth true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function edit()
    {
        $this->_form(ConfigKitchenToolModel::class, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            $data['nid'] = intval($data['nid'] ?? input('nid', '0'));
            $cates = (new ConfigKitchenToolModel)->where(['is_deleted' => 0])->order('id asc')->select();
            $this->cates = DataExtend::arr2table(array_merge($cates->toArray(), [['id' => '0', 'nid' => '-1', 'name' => '顶部分类']]), 'id', 'nid');
            if (isset($data['id'])) foreach ($this->cates as $cate) if ($cate['id'] === $data['id']) $data = $cate;
            foreach ($this->cates as $key => $cate) if ((isset($data['spt']) && $data['spt'] <= $cate['spt'])) {
                unset($this->cates[$key]);
            }
        }
    }

    /**
     * 修改共享厨房工具状态
     * @auth true
     * @throws DbException
     */
    public function state()
    {
        $this->_save(ConfigKitchenToolModel::class, $this->_vali([
            'is_modular.in:0,1' => '状态值范围异常！',
            'is_modular.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 修改共享厨房工具状态
     * @auth true
     * @throws DbException
     */
    public function sort()
    {
        $this->_save(ConfigKitchenToolModel::class);
    }

    /**
     * 删除共享厨房工具
     * @auth true
     * @throws DbException
     */
    public function remove()
    {
        $this->_delete(ConfigKitchenToolModel::class);
    }
}